package com.holy.sh.module_base_mvp.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.holy.sh.module_base_mvp.utils.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Holy Email :victorhhl@163.com
 * Create by Holy on 2019/8/14
 * Description :
 */
public class BasicLineChartView extends View {
    public static String TAG = "BasicLineChartView";
    private Paint mPaXZ, mPaYZ, mPaLine, mPaPoint, mXtextPaint, mYtextPaint;
    float yAverageLength, xAverageLength;
    float xLength, yLength;
    List<Float> yValue;
    float yMax;//传入数据Y最大值
    List<Float> xPointValue;
    Path path;
    float xTotext = 20, yTotext = 50;//文字到坐标轴之间的距离
    List<String> xText;

    public BasicLineChartView(Context context) {
        super(context);
        initOp();
    }


    public BasicLineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initOp();
    }


    public BasicLineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initOp();
    }

    private void initOp() {
        yValue = new ArrayList<>();
        xPointValue = new ArrayList<>();
        yValue.add(100f);
        yValue.add(300f);
        yValue.add(600f);
        yValue.add(500f);
        yValue.add(700f);
        yValue.add(300f);
        yValue.add(200f);

        xText = new ArrayList<>();
        xText.add("Mon");
        xText.add("Tue");
        xText.add("Web");
        xText.add("Thu");
        xText.add("Fri");
        xText.add("Sat");
        xText.add("Sun");


        mPaXZ = new Paint();
        mPaXZ.setColor(Color.BLACK);
        mPaXZ.setStyle(Paint.Style.STROKE);
        mPaXZ.setStrokeWidth(4);
        mPaXZ.setAntiAlias(true);

        mPaYZ = new Paint();
        mPaYZ.setColor(Color.BLACK);
        mPaYZ.setStyle(Paint.Style.STROKE);
        mPaYZ.setStrokeWidth(4);
        mPaYZ.setAntiAlias(true);

        mPaLine = new Paint();
        mPaLine.setColor(Color.RED);
        mPaLine.setStyle(Paint.Style.STROKE);
        mPaLine.setStrokeWidth(5);
        //mPaLine.setShadowLayer(5);
        mPaLine.setPathEffect(new CornerPathEffect(50));
        mPaLine.setAntiAlias(true);

        mPaPoint = new Paint();
        mPaPoint.setColor(Color.RED);
        mPaPoint.setStyle(Paint.Style.STROKE);
        mPaPoint.setStrokeWidth(1);
        mPaPoint.setAntiAlias(true);


        path = new Path();

        mXtextPaint = new Paint();
        mXtextPaint.setColor(Color.BLACK);
        mXtextPaint.setStyle(Paint.Style.STROKE);
        mXtextPaint.setStrokeWidth(1);
        mXtextPaint.setAntiAlias(true);
        //Paint 字体相关
        mXtextPaint.setTextSize(30);
        mXtextPaint.setFakeBoldText(false);//加粗
        mXtextPaint.setStrikeThruText(false);//删除线
        mXtextPaint.setUnderlineText(false);//下划线


        mYtextPaint = new Paint();
        mYtextPaint.setColor(Color.BLACK);
        mYtextPaint.setStyle(Paint.Style.STROKE);
        mYtextPaint.setStrokeWidth(1);
        mYtextPaint.setAntiAlias(true);
        //Paint 字体相关
        mYtextPaint.setTextSize(30);
        mYtextPaint.setFakeBoldText(false);//加粗
        mYtextPaint.setStrikeThruText(false);//删除线
        mYtextPaint.setUnderlineText(false);//下划线
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHight = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHightMode = MeasureSpec.getMode(heightMeasureSpec);

        setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth : getWidth(),
                (measureHightMode == MeasureSpec.EXACTLY) ? measureHight : getHeight());

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        LogUtils.i("canvas", "getWidth-->", "" + getWidth());
        LogUtils.i("canvas", "getHeight-->", "" + getHeight());

        xLength = getWidth() - xTotext;
        yLength = getHeight() - yTotext;

        xAverageLength = (xLength - (xLength % yValue.size())) / yValue.size();
        yMax = Collections.max(yValue);
        yAverageLength = (yLength - 20) / yMax;
        LogUtils.i(TAG, "yMax-->", "" + yMax);

        // if (yMax%)

        for (int i = 0; i < yValue.size(); i++) {
            float yHeight = yAverageLength * yValue.get(i);//每个点的y高度
            xPointValue.add((float) ((i * xAverageLength) + (xAverageLength / 2)));
            canvas.drawText(xText.get(i), (i * xAverageLength) + (xAverageLength / 2) - (getTextPy(mXtextPaint, xText.get(i))), getHeight(), mXtextPaint);//画x轴文字
            float v = yLength - (yHeight + 20 + getTextHeight(mXtextPaint, yValue.get(i) + ""));
            if (v < 0) {
                canvas.drawText(yValue.get(i) + "", (i * xAverageLength) + (xAverageLength / 2), yLength - yHeight + 20, mXtextPaint);
            } else {
                canvas.drawText(yValue.get(i) + "", (i * xAverageLength) + (xAverageLength / 2), yLength - yHeight - 20, mXtextPaint);
            }
            canvas.drawCircle((i * xAverageLength) + (xAverageLength / 2), yLength - yHeight, 5, mPaPoint);//画圆点
            if (i>0){
                canvas.drawLine(i * xAverageLength, yLength, i * xAverageLength, yLength + 5, mPaXZ);
            }

            getLinePath(i);
        }
        canvas.drawPath(path, mPaLine);
        canvas.drawLine(0, 20, 0, yLength, mPaYZ);
        canvas.drawLine(0, yLength, xLength, yLength, mPaXZ);
    }


    private void getLinePath(int i){

            if (i != 0) {//画path
                path.lineTo((i * xAverageLength) + (xAverageLength / 2), yLength - (yAverageLength * yValue.get(i)));
                /*if (i > 0 && i < yValue.size() - 1) {
                    float hx = (i + 1) * xAverageLength;
                    float hy = yLength - (yAverageLength * yValue.get(i) + yAverageLength * yValue.get(i + 1)) / 2;
                    float tx = i * xAverageLength + (xAverageLength / 2);
                    float ty = yLength - (yAverageLength * yValue.get((i)));
                    LogUtils.i("quadTo", "onePosition-->" + "( " + hx + " , " + hy + " )", "toPosition-->" + "( " + tx + " , " + ty + " )");
                    path.quadTo(tx, ty, hx, hy);
                } else if (i==yValue.size()-1){
                    float hx = xLength;
                    float hy = yLength - (yAverageLength * yValue.get(i)) / 2;
                    float tx = i * xAverageLength + (xAverageLength / 2);
                    float ty = yLength - (yAverageLength * yValue.get((i)));
                    path.quadTo(tx,ty,xLength,hy);
                }*/


                LogUtils.i(TAG, "PointX-->", String.valueOf((i * xAverageLength) - (xAverageLength / 2)));
                LogUtils.i(TAG, "PointY-->", "" + yAverageLength * yValue.get(i));
            } else {
                float x1 = xAverageLength / 2;
                // float y1=(yAverageLength*yValue.get(i));
                float y1 = yLength - (yAverageLength * yValue.get(i));
                float x2 = xAverageLength;
                float y2 = yLength - (yAverageLength * yValue.get(i) + yAverageLength * yValue.get(i + 1)) / 2;
                path.moveTo(0, yLength - (yAverageLength * yValue.get(i)) / 2);
                path.lineTo(x1, y1);
                //  path.quadTo(x1,y1, x2,y2);
                LogUtils.i("quadTo0", "onePosition-->" + "( " + x1 + " , " + y1 + " )", "toPosition-->" + "( " + x2 + " , " + y2 + " )");
                LogUtils.i(TAG, "PointX-->", "" + (xAverageLength / 2));
                LogUtils.i(TAG, "PointY-->", "" + yAverageLength * yValue.get(i));

            }
        }







    private float getTextPy(Paint mPaint, String text) {
        return mPaint.measureText(text) / 2;
    }

    private float getTextHeight(Paint mPaint, String text) {
        Rect rect = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), rect);
        int w = rect.width();
        int h = rect.height();
        LogUtils.i(TAG, "heightText", "" + h);
        return h;
    }
}
